/*
 * td1.cpp
 *
 *  Created on: 16 janv. 2012
 *      Author: guillaume Bouhier, anne-elise Poirot
 */


#include "td1.hpp"


/**
 * Convention :
 * SIZE_IN est toujours la taille du tableau d'entree
 */

void interpolation_facteur2(double* entree, double* sortie, const unsigned int & SIZE_IN) {

	// On vérifie que le signal de sortie est bien 2 fois plus grand que le signal d'entrée

	for(unsigned int cpt =0; cpt < SIZE_IN; cpt++){
		sortie[2*cpt] = entree[cpt];
		sortie[2*cpt+1] = 0;
	}
}

void decimation_facteur2(double* entree, double* sortie, const unsigned int & SIZE_IN) {
	for(unsigned int cpt =0; cpt < SIZE_IN/2; cpt++){
		sortie[cpt] = entree[2*cpt];
	}
}

void convolution(double* entree, double* h, const unsigned int & SIZE_IN, const unsigned int & q){

	double* temp = new double[SIZE_IN]();

	// assert : q est bien de taille impaire
	assert(q%2 == 1 && "Q n'est pas de taille impair");


	// cas où on se trouve sur les bords : on regarde les indices en miroir
	for(unsigned int n=0; n<SIZE_IN; n++) {
		temp[n] = 0;
		int ind = 0;
		// boucle sur valeurs de k
		for(unsigned int k = 0; k < q; k++){

			ind = n-(k-floor(q/2));

			if(ind < 0) ind += SIZE_IN;
			if(ind > SIZE_IN-1) ind -= SIZE_IN;
		//	if(ind < 0) ind = -ind;
		//	if(ind > SIZE_IN-1) ind -= (ind - SIZE_IN -1);
			temp[n] += h[k] * entree[ind];

		}
	}


	memcpy(entree, temp, SIZE_IN*sizeof(double));

	delete[] temp;

	/*for(unsigned int i = 0; i<256; i++ ){

          std::cout <<	entree[i] << std::endl;
         }*/

}

void analyse_haar(double* x, unsigned int p, FILE* file){
	double _h0[3] = {1/sqrt(2), 1/sqrt(2), 0};
	double _h1[3] = {1/sqrt(2), -1/sqrt(2), 0};

	double* xb = new double[p];
	double* xh = new double[p];
	memcpy(xb, x, p*sizeof(double));
	memcpy(xh, x, p*sizeof(double));

	double* xbd = new double[p/2]();
	double* xhd = new double[p/2]();

	convolution(xb, _h0, p, 3);
	convolution(xh, _h1, p, 3);

	decimation_facteur2(xb, xbd, p);
	decimation_facteur2(xh, xhd, p);

	memcpy(x, xbd, p*sizeof(double)/2);
	memcpy(&x[p/2], xhd, p*sizeof(double)/2);

	for(int i = 0; i< p; i++) {

		// std::cout << "Coef num "<<  i << " : " << x[i] << std::endl;
		fprintf (file, "%e\n",x[i]);
	}


	delete[] xb;
	delete[] xh;

}

void synthese_haar(double*x, unsigned int p, FILE* file){

	double* xbd = new double[p/2];
	double* xhd = new double[p/2];
	double* y = new double[p];

	memcpy(xbd, x, p*sizeof(double)/2);
	memcpy(xhd, x+p/2, p*sizeof(double)/2);

	/**std::cout << xbd[0] << std::endl;
        std::cout << xhd[0] << std::endl;**/

	double* xbi = new double[p];
	double* xhi = new double[p];

	double _g0[3] = {0, 1/sqrt(2), 1/sqrt(2)};
	double _g1[3] = {0, -1/sqrt(2), 1/sqrt(2)};
	unsigned int q = 3;

	interpolation_facteur2(xbd, xbi, p/2);
	interpolation_facteur2(xhd, xhi, p/2);
	convolution(xbi, _g0, p, q);
	convolution(xhi, _g1, p, q);

	double*/*
	 * td2.cpp
	 *
	 *  Created on: 29 janv. 2012
	 *      Author: guillaume
	 */

 &yb = xbi;
	double* &yh = xhi;

	sommation(y, yb, yh, p);

	for(int i = 0; i< p; i++) {

		// std::cout << "Coef num "<<  i << " : " << y[i] << std::endl;
		fprintf (file, "%e\n",y[i]);
	}

	delete[] xbd;
	delete[] xhd;
	delete[] xbi;
	delete[] xhi;

}

void sommation(double* y, double* yb, double* yh, unsigned int SIZE_Y){

	for(unsigned int i = 0; i <SIZE_Y; i++){

		y[i] = yb[i] + yh[i];
	}

}
void chargeCoef(double* entree, unsigned int SIZE_IN, FILE* file){

	float temp = 0.0;
	for(unsigned int i = 0; i<SIZE_IN; i++){

		if(file != NULL) fscanf(file, "%f", &temp);
		//cout << "read " <<  x_leleccum_f << endl;
		entree[i] = static_cast<double>(temp);
	}
}

void analyse_97(double*x, unsigned int p, FILE* file){

	double _h0[9];
	_h0[0]=0.037828455507;
	_h0[1]=-0.023849465019;
	_h0[2]=-0.110624404418;
	_h0[3]=0.377402855613;
	_h0[4]=0.852698679009;
	_h0[5]=0.377402855613;
	_h0[6]=-0.110624404418;
	_h0[7]=-0.023849465019;
	_h0[8]=0.037828455507;

	double _h1[9];
	_h1[0]=0.064538882629;
	_h1[1]=-0.040689417610;
	_h1[2]=-0.418092273222;
	_h1[3]=0.788485616406;
	_h1[4]=-0.418092273222;
	_h1[5]=-0.040689417610;
	_h1[6]=0.064538882629;
	_h1[7]=0.000000000000;
	_h1[8]=-0.000000000000;

	double* xb = new double[p];
	double* xh = new double[p];
	memcpy(xb, x, p*sizeof(double));
	memcpy(xh, x, p*sizeof(double));

	double* xbd = new double[p/2]();
	double* xhd = new double[p/2]();

	convolution(xb, _h0, p, 9);
	convolution(xh, _h1, p, 9);

	decimation_facteur2(xb, xbd, p);
	decimation_facteur2(xh, xhd, p);

	memcpy(x, xbd, p*sizeof(double)/2);
	memcpy(&x[p/2], xhd, p*sizeof(double)/2);

	for(int i = 0; i< p; i++) {

		// std::cout << "Coef num "<<  i << " : " << x[i] << std::endl;
		fprintf (file, "%e\n",x[i]);
	}

	delete[] xb;
	delete[] xh;

}

void synthese_97(double*x, unsigned int p, FILE* file){

	double* xbd = new double[p/2];
	double* xhd = new double[p/2];
	double* y = new double[p];

	memcpy(xbd, x, p*sizeof(double)/2);
	memcpy(xhd, x+p/2, p*sizeof(double)/2);

	/**std::cout << xbd[0] << std::endl;
	   std::cout << xhd[0] << std::endl;**/

	double* xbi = new double[p];
	double* xhi = new double[p];

	double _g0[7];
	_g0[0]=-0.064538882629;
	_g0[1]=-0.040689417610;
	_g0[2]=0.418092273222;
	_g0[3]=0.788485616406;
	_g0[4]=0.418092273222;
	_g0[5]=-0.040689417610;
	_g0[6]=-0.064538882629;

	double _g1[11];
	_g1[0]=0.000000000000;
	_g1[1]=-0.000000000000;
	_g1[2]=0.037828455507;
	_g1[3]=0.023849465019;
	_g1[4]=-0.110624404418;
	_g1[5]=-0.377402855613;
	_g1[6]=0.852698679009;
	_g1[7]=-0.377402855613;
	_g1[8]=-0.110624404418;
	_g1[9]=0.023849465019;
	_g1[10]=0.037828455507;

	//unsigned int q = 3;

	interpolation_facteur2(xbd, xbi, p/2);
	interpolation_facteur2(xhd, xhi, p/2);
	convolution(xbi, _g0, p, 7);
	convolution(xhi, _g1, p, 11);

	double* &yb = xbi;
	double* &yh = xhi;

	sommation(y, yb, yh, p);

	for(unsigned int i = 0; i< p; i++) {

		// std::cout << "Coef num "<<  i << " : " << y[i] << std::endl;
		fprintf (file, "%e\n",y[i]);
	}

	delete[] xbd;
	delete[] xhd;
	delete[] xbi;
	delete[] xhi;


}


